#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        vector<vector<int>> ret;
        for (int i = 0; i < n - 2; )
        {
            int target = -nums[i];
            int left = i + 1, right = n - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] < target)
                {
                    left++;
                }
                else if (nums[left] + nums[right] == target)
                {
                    ret.push_back({ nums[i], nums[left], nums[right] });
                    int tmp = nums[left];
                    while (left < right && nums[left] == tmp)   left++;
                    tmp = nums[right];
                    while (left < right && nums[right] == tmp)  right--;

                }
                else
                {
                    right--;
                }
            }
            int tmp = nums[i];
            while (i < n - 2 && nums[i] == tmp)  i++;
        }

        return ret;
    }
};